]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
extensionUtils: Stop using RegExp for stack parsing
authorFlorian Müllner <fmuellner@gnome.org>
Sun, 9 Jul 2023 23:20:53 +0000 (01:20 +0200)
committerMarge Bot <marge-bot@gnome.org>
Sat, 15 Jul 2023 11:07:12 +0000 (11:07 +0000)
We currently use a regular expression to extract the exact file path
from a stack line. That RE is no longer accurate:

 - we assume a line number at the end, but at one point the column
   number was added as well
 - stacks from ES modules use file:// URIs instead of plain paths

Luckily that doesn't matter: We don't want to access any actual
files, so all we need is a path that can be traversed and that
contains the UUID.

We can get that with simple string manipulation, so avoid the regex
overhead.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2832>

js/misc/extensionUtils.js

index df43d9825775fe966db1dfd32f28856dd5dc591b..c60c4520c104ee4e8c97b8b6c553bcdf7ed56975 100644 (file)
@@ -77,35 +77,28 @@ function getCurrentExtension() {
     if (_extension)
         return _extension;
 
-    let stack = new Error().stack.split('\n');
-    let extensionStackLine;
+    const basePath = '/gnome-shell/extensions/';
 
     // Search for an occurrence of an extension stack frame
     // Start at 1 because 0 is the stack frame of this function
-    for (let i = 1; i < stack.length; i++) {
-        if (stack[i].includes('/gnome-shell/extensions/')) {
-            extensionStackLine = stack[i];
-            break;
-        }
-    }
-    if (!extensionStackLine)
-        return null;
+    const [, ...stack] = new Error().stack.split('\n');
+    const extensionLine = stack.find(
+        line => line.includes(basePath));
 
-    // The stack line is like:
-    //   init([object Object])@/home/user/data/gnome-shell/extensions/u@u.id/prefs.js:8
-    //
-    // In the case that we're importing from
-    // module scope, the first field is blank:
-    //   @/home/user/data/gnome-shell/extensions/u@u.id/prefs.js:8
-    let match = new RegExp('@(.+):\\d+').exec(extensionStackLine);
-    if (!match)
+    if (!extensionLine)
         return null;
 
     // local import, as the module is used from outside the gnome-shell process
     // as well (not this function though)
     let extensionManager = imports.ui.main.extensionManager;
 
-    let path = match[1];
+    // The exact stack line differs depending on where the function
+    // was called (function or module scope), and whether it's called
+    // from a module or legacy import (file:// URI vs. plain path).
+    //
+    // We don't have to care about the exact composition, all we need
+    // is a string that can be traversed as path and contains the UUID
+    const path = extensionLine.slice(extensionLine.indexOf(basePath));
     let file = Gio.File.new_for_path(path);
 
     // Walk up the directory tree, looking for an extension with